//
//  ZDJingXuanTopHeadView.m
//  1012GiftTips
//
//  Created by Apple on 15/10/15.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDJingXuanTopHeadView.h"
#import "ZDJingXuanButton.h"
#import "UIImage+GetColor.h"

#define ZDADCount 6
#define ZDBtnCount 4
#define scrollViewWidth ZDScreenSize.width

@interface ZDJingXuanTopHeadView () <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *topHeadScroll;

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@property (nonatomic, strong) NSTimer *timer;


@end

@implementation ZDJingXuanTopHeadView


+ (instancetype)jingXuanTopHeadView
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:nil options:nil] lastObject];
}


- (void)awakeFromNib
{
    
/*  awakeFromNib方法中拿到的是对应nib文件中控件的size大小，并不是最终控件显示的大小  */
    
    [super awakeFromNib];
    
    //添加scrollView
    [self setUpScrollView];
    
//    ZDLog(@"%@", self.subviews);
    
    //添加按钮
    [self setUpButtons];
}

#pragma mark - 添加产品推荐按钮
- (void)setUpButtons
{
    NSArray *titleArray = @[@"美好小物", @"好店推荐", @"每日签到", @"天天刮奖"];
    
    for (UIView *view in self.subviews)
    {
        if ([view isKindOfClass:[ZDJingXuanButton class]])
        {
            ZDJingXuanButton *btn = (ZDJingXuanButton *)view;
            
            UIImage *image = nil;
            
            UIColor *color = nil;
            
            switch (btn.tag)
            {
                case 0:
                    image = [UIImage imageNamed:@"Share_QQIcon"];
                    color = [image pixelColorAtLocation:CGPointMake(10, image.size.height * 0.5)];
                    [btn setImage:image forState:UIControlStateNormal];
                    [btn setTitle:titleArray[0] forState:UIControlStateNormal];
                    break;
                case 1:
                    image = [UIImage imageNamed:@"Share_QzoneIcon"];
                    color = [image pixelColorAtLocation:CGPointMake(10, image.size.height * 0.5)];
                    [btn setImage:image forState:UIControlStateNormal];
                    [btn setTitle:titleArray[1] forState:UIControlStateNormal];
                    break;
                case 2:
                    image = [UIImage imageNamed:@"Share_WeChatSessionIcon"];
                    color = [image pixelColorAtLocation:CGPointMake(10, image.size.height * 0.5)];
                    [btn setImage:image forState:UIControlStateNormal];
                    [btn setTitle:titleArray[2] forState:UIControlStateNormal];
                    break;
                case 3:
                    image = [UIImage imageNamed:@"Share_WeChatTimelineIcon"];
                    color = [image pixelColorAtLocation:CGPointMake(10, image.size.height * 0.5)];
                    [btn setImage:image forState:UIControlStateNormal];
                    [btn setTitle:titleArray[3] forState:UIControlStateNormal];
                    break;
                default:
                    break;
            }
            
            [btn setTitleColor:color forState:UIControlStateNormal];
            
            [self addSubview:btn];
        }
    }
}


//#pragma mark - 布局子控件frame  ———— 改成在storyBoard设置约束
//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//    
//   
//}

#pragma mark - 产品推荐按钮点击事件
- (IBAction)jingXuanBtnClick:(UIButton *)sender
{
    if (self.jingPinBtnClickBlock)
    {
        self.jingPinBtnClickBlock(sender.tag);
    }
}



#pragma mark - 设置循环轮播器
- (void)setUpScrollView
{
    //设置循环轮播器
    
    CGFloat imageY = 0;
    CGFloat imageW = scrollViewWidth;
    CGFloat imageH = self.frame.size.height;
    
    for (int i = 0; i < ZDADCount; i++)
    {
        UIImageView *imageView = [[UIImageView alloc] init];
        
        CGFloat imageX = (i+1) * imageW;
        
        imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
        
        NSString *imageName = [NSString stringWithFormat:@"GS_%02d_320.jpg", i+1];
        
        imageView.image = [UIImage imageNamed:imageName];
        
        [self.topHeadScroll addSubview:imageView];
    }
    
    //在开头插入最后一张图片
    UIImageView *imageViewFirst = [[UIImageView alloc] init];
    
    imageViewFirst.frame = CGRectMake(0, 0, imageW, imageH);
    
    imageViewFirst.image = [UIImage imageNamed:@"GS_06_320.jpg"];
    
    [self.topHeadScroll addSubview:imageViewFirst];
    
    //在结尾插入第一张图片
    UIImageView *imageViewLast = [[UIImageView alloc] init];
    
    imageViewLast.frame = CGRectMake((ZDADCount + 1)*imageW, 0, imageW, imageH);
    
    imageViewLast.image = [UIImage imageNamed:@"GS_01_320.jpg"];
    
    [self.topHeadScroll addSubview:imageViewLast];
    
    
    //设置一开始显示第二张图片
    [self.topHeadScroll setContentOffset:CGPointMake(imageW, 0) animated:YES];
    
    //设置scrollView的contentSize
    self.topHeadScroll.contentSize = CGSizeMake((ZDADCount + 2) * imageW, 0);
    
    //取消水平滚动条
    self.topHeadScroll.showsHorizontalScrollIndicator = NO;
    
    //设置分页
    self.topHeadScroll.pagingEnabled = YES;
    
    //设置代理
    self.topHeadScroll.delegate = self;
    
    //设置定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(scroll) userInfo:nil repeats:YES];
    
    //添加到runloop中
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

//回调方法 自动滚动
- (void)scroll
{
    //获取当前scrollView的偏移
    CGFloat offsetX = self.topHeadScroll.contentOffset.x;
    
    //计算索引
    NSInteger index = offsetX / scrollViewWidth + 0.5;
    
    index++;
    
    //滚动到下一页
    [self.topHeadScroll setContentOffset:CGPointMake(index * scrollViewWidth, 0) animated:YES];
}


#pragma mark - UIScrollView代理方法
//MARK: -- scrollView滚动(偏移发生改变就会调用)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取当前scrollView的偏移
    CGFloat offsetX = self.topHeadScroll.contentOffset.x;
    
    //计算索引
    NSInteger index = offsetX / scrollViewWidth + 0.5;
    
    //    ZDLog(@"%ld", index);
    
    if (index == ZDADCount + 1)
    {
        index = 1;
    }
    else if (index == 0)
    {
        index = ZDADCount;
    }
    
    self.pageControl.currentPage = index - 1;
}

//MARK: -- scrollView开始拖拽的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    
    self.timer = nil;
}

//MARK: -- scrollView结束拖拽的时候调用
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //设置定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(scroll) userInfo:nil repeats:YES];
    
    //添加到runloop中
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

//MARK: -- scrollView结束减速的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //获取当前scrollView的偏移
    CGFloat offsetX = self.topHeadScroll.contentOffset.x;
    
    //计算索引
    NSInteger index = offsetX / scrollViewWidth + 0.5;
    
    //    ZDLog(@"scrollViewDidEndDecelerating %ld", index);
    
    if (index == ZDADCount + 1)
    {
        [self.topHeadScroll setContentOffset:CGPointMake(scrollViewWidth, 0) animated:NO];
    }
    else if (index == 0)
    {
        [self.topHeadScroll setContentOffset:CGPointMake(ZDADCount * scrollViewWidth, 0) animated:NO];
    }
}

//MARK: -- scrollView结束滚动动画的时候调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollViewDidEndDecelerating:scrollView];
}

- (void)dealloc
{
    ZDLog(@"headerView释放了");
}


@end
